﻿package mortal.game.view.copy.crossMainCity.panels.data
{
    import Message.Public.*;
    import com.gengine.global.*;
    import flash.utils.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.mvc.core.*;

    public class MainCityFinalOccupyTime extends Object
    {
        private var _orriginalSeconds:int = -1;
        private var _occupyGuildId:SEntityId = null;
        private var _occupyTime:int = -1;
        private var _resultSeconds:int = 0;
        private static var _instance:MainCityFinalOccupyTime;

        public function MainCityFinalOccupyTime()
        {
            return;
        }// end function

        public function dispose() : void
        {
            Global.instance.removeEnterSecond(this.everySecond);
            return;
        }// end function

        public function pickFlag(param1:SMainCityBattle2MiniGuildMsg) : void
        {
            this._orriginalSeconds = Cache.instance.mainCity.getFinalOccupyTime(param1.guildId);
            this._occupyTime = getTimer();
            this._occupyGuildId = param1.guildId;
            this._resultSeconds = this._orriginalSeconds;
            this.everySecond();
            Global.instance.addEnterSecond(this.everySecond);
            return;
        }// end function

        public function lostFlag() : void
        {
            this.everySecond();
            this._orriginalSeconds = 0;
            this._resultSeconds = 0;
            this._occupyGuildId = null;
            this._occupyTime = -1;
            Global.instance.removeEnterSecond(this.everySecond);
            return;
        }// end function

        private function everySecond() : void
        {
            if (this._occupyGuildId == null || this._occupyTime < 0)
            {
                return;
            }
            var _loc_1:* = Math.floor((getTimer() - this._occupyTime) / 1000);
            if (_loc_1 <= 0)
            {
                return;
            }
            this._resultSeconds = this._orriginalSeconds + _loc_1;
            Cache.instance.mainCity.updateFinalOccupyTime(this._occupyGuildId, this._resultSeconds);
            Dispatcher.dispatchEvent(new DataEvent(EventName.MainCityFinalOccupyTimeChanged));
            return;
        }// end function

        public static function get instance() : MainCityFinalOccupyTime
        {
            if (_instance == null)
            {
                _instance = new MainCityFinalOccupyTime;
            }
            return _instance;
        }// end function

        public static function get hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
